package com.youth.controller.fileupload;

import java.io.File;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MaxUploadSizeExceededException;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

@Controller
@RequestMapping("")
public class FileUploadController  {
	
	@Value("#{prop['upload.path']}")
	String uploadPath;
	
	private static Logger logger =Logger.getLogger(FileUploadController.class);
	
	@RequestMapping("/upload")
	public String fileUpload(@RequestParam("myFile") MultipartFile multipartFile) throws Exception{
		String fileName =  multipartFile.getOriginalFilename();
		File f = createFile(fileName);
		multipartFile.transferTo(f);
		return "redirect:/fileUpload.jsp";
	}
	
	@RequestMapping("ajaxUpload")
	public void ajaxFileUpload(@RequestParam("myFile") MultipartFile multipartFile) throws Exception{
		String fileName =  multipartFile.getOriginalFilename();
		File f = createFile(fileName);
		multipartFile.transferTo(f);
	}
	
	@RequestMapping("uploadMaxFile")
	public String uploadMaxFile(@RequestParam("myFile") MultipartFile multipartFile) throws Exception{
		String fileName =  multipartFile.getOriginalFilename();
		File f = createFile(fileName);
		multipartFile.transferTo(f);
		return "redirect:/fileUpload.jsp";
	}

	@RequestMapping("/uploadMaxFile2")
	public String uploadMaxFile2(HttpServletRequest request, Model model) throws Exception{
		try {
			MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;  
			MultipartFile multipartFile = multipartRequest.getFile("myFile"); 
			String fileName = multipartFile.getOriginalFilename(); 
			File f = createFile(fileName);
			multipartFile.transferTo(f);
			model.addAttribute("info", "上传成功！");
			logger.info(fileName);
		} catch (MaxUploadSizeExceededException musee) {
			// 文件过大时捕获异常，为什么文件过大会连续请求controller？？？
			String str = "文件大小不应该大于" + musee.getMaxUploadSize()/1024 + "KB";
			model.addAttribute("info", str);
			throw musee;
		}
		return "fileUpload";
	}
	
	private File createFile(String fileName) {
		File f = new File(uploadPath + fileName);
		if (!f.getParentFile().exists()) {
			f.getParentFile().mkdirs();
		}
		return f;
	}
	
	@ExceptionHandler(MaxUploadSizeExceededException.class)
	public String handleException(MaxUploadSizeExceededException ex) throws Exception {
		return "redirect:/MaxUploadSizeError.jsp?size=" + ex.getMaxUploadSize();
	}
	
	@SuppressWarnings("null")
	@RequestMapping("exceptionCatch")
	public void exceptionCatch(){
		String str = null;
		str.length();
	}
	
	/**
	 * 监控文件上传进度
	 * @return
	 */
	@RequestMapping("/getUploadProcess")
	public void getUploadProcess(HttpSession session, PrintWriter out) {
		Object obj = session.getAttribute("progress");
		System.out.println("上传进度" + obj);
		if (null != obj) {
			String percent = obj.toString();
			out.print(percent);
			out.flush();
			out.close();
		}
	}
	
}
